/*
**NCS configure head file for read or write database
**
**This module provide the interface of reading/writing 
**the database. 
**
**
**Copyright (C) 2016-1 andisat <www.andisat.com>
**
**Author: andisat
**Date:2016-1
**
*/

#ifndef __NCS_DATABASE_CFG__
#define __NCS_DATABASE_CFG__

#include <time.h>
#include "ncs_dev.h"
#include "ncs_db.h"

#define DB_LOCK TRUE
#define DB_UNLOCK FALSE
#define DB_MSG_LEN 64

#define MSGTYPE_MAX 256
#define QOS_RULE_MAX 256


enum ENUM_BACKUP_STATE{
    BACKUP_MASTER=0,
    BACKUP_SLAVE
};

enum ENUM_CARRIER_TYPE{
    CARRIER_TYPE_IB=1,
    CARRIER_TYPE_TDM,
    CARRIER_TYPE_DAMA
};

enum ENUM_CFG2DB_TYPE{
    _CFG2DB_NULL_ = 0,
    _IPPORT_ = 1,
    _IPADDR_ = 2,
    _POLLING_ = 3,
    _CARRIER_ = 4,
    _MODEM_ = 5,
    _USER_ID_ = 6,
    _WORK_MODE_ = 7,
    _RESOURCE_BOD_ = 8,
    _RESOURCE_RULE_ITEMS_ = 9,
    _ADD_ROUTER_ = 10,
    _UPDATE_ROUTER_ = 11,
    _DEL_ROUTER_ = 12,
    _CHANNEL_ID_ = 13,
    _CHANNEL_COMM_ = 14,
    _CHANNEL_ = 15,
    _ADD_QOSRULE_ITEMS_ = 16,
    _DEL_QOSRULE_ITEMS_ = 17,
    _UPDATE_QOSRULE_ITEMS_ = 18,
    _DEV_STATUS_ = 19,
    _CHANNEL_STATUS_ = 20,
    _RESOURCE_STATUS_ = 21,
    _TRAFFIC_STATIC_ = 22,
    _VLAN_ = 23,
    _DEFAULT_GWIP_ = 24,
    _DEV_NAME_ = 25,
    _DEV_TIME_ = 26,
    _SYSTEM_OP_ = 27,
    _LICAUTH_MODE_ = 28,
    _RESOURCE_ = 29,
    _LICENSE_INFO_ = 30,
    _DEV_ID_ = 31,
    _BACKUP_ = 32,
    _ALARM_ = 33,
    _EQUIPMENT_FAULT_ = 34,
    _MULTICAST_01_ = 35,
    _MULTICAST_02_ = 36,
    _MULTICAST_03_ = 37,
    _MULTICAST_04_ = 38,
    _MULTICAST_05_ = 39,
    _CFG_RESULT_ = 40,
    _RESULT_ = 41,
    _NCS_STATE_=180,
    _PP_STATE_,
    _MODEM_STATE_,
    _CFG2DB_MAX_ = 255
};

#pragma pack(1)
struct _STR_IPPORT_{
    int ip;
    short port;
};

struct _STR_IPADDR_{
    int ip;  
    char maskLen;
    char ifname;
};

struct _STR_NETWORK_{
    char type;
    int  ip;
    char mask;
};

struct _STR_POLLING_{
    short poll_interval;
    short poll_maxmodem;
};

struct _STR_CARRIER_{
    int  carrierID;
    char type;
    int  bw;
    int  centerFreq;
    short power;
    char modulation;
    char fec;
    char fecRate;
    char alpha;
};

struct _STR_MODEM_{
    char type;
    short typecode;  /*dev ability*/
    int ip; 
    char mask;
    int satIp;
    int devId;
    char devSN[9];
};

struct _STR_USER_ID_{
    int user_id;
};

struct _STR_WORK_MODE_{
    char workMod;
    char resMod;
    char qosMod;
    int closeTime;
};

struct _STR_RESOURCE_BOD_{
    int initBW;
    int step; 
    char maxStep;
    char enable;
};

struct _STR_RESOURCE_RULE_ITEMS_{
    char rule[192]; 
};

struct _STR_ADD_ROUTER_{
    int dstip;
    char masklen;
    int gateway;
    char ifname[20];
    short metric;
};

struct _STR_UPDATE_ROUTER_{
    int old_dstip;
    char old_masklen;
    int old_gateway;
    char old_ifname[20];
    short old_metric;
    int dstip;
    char masklen;
    int gateway;
    char ifname[20];
    short metric;
};
struct _STR_DEL_ROUTER_{
    int dstip;
    char masklen;
    int gateway;
    char ifname[20];
    short metric;
};

struct _STR_CHANNEL_ID_{
    int devId;
    char chaId;
};

struct _STR_CHANNEL_COMM_{
    char type;
    int cf_freq;
    char filterMode;
    char fliterBW;
};

struct _STR_CHANNEL_{
    char id;
    char type;
    int freq;
    int bw;
    short power;
    int sweepRange;
    char modulation;
    char fec;
    char fecRate;           
    char ncr;
    char freqRvs;
    char rollingFactor;	
    int txChaId;
};


struct _STR_ADD_QOSRULE_ITEMS_{
    char rule[QOS_RULE_MAX];
};

struct qosrule_items{
    int len;
    struct _STR_ADD_QOSRULE_ITEMS_ items;
};

struct _STR_DEL_QOSRULE_ITEMS_{
    short id;
};
struct _STR_UPDATE_QOSRULE_ITEMS_{
    char rule[QOS_RULE_MAX];
};

struct _STR_DEV_STATUS_{
    char status;
};

struct _STR_CHANNEL_STATUS_{
    int devId;
    char chaId;
    int power;
    int ebn;
    int rate;
};

struct _STR_RESOURCE_STATUS_{
    int rxdevId;
    char rxchaId;
    int txdevId;
    char txchaId;
    int freq;
    int BW;
};

struct _STR_TRAFFIC_STATIC_{
    int rxdevId;
    char rxchaId;
    int txdevId;
    char txchaId;
    int freq;
    int BW;
};

#define VLAN_MAX 300
struct _STR_VLAN_{
    short cnt;
    short vlan[VLAN_MAX];
};

struct _STR_DEFAULT_GWIP_{
    int gateway;
};

#define DEV_NAME_MAX 65
struct _STR_DEV_NAME_{
    char name[DEV_NAME_MAX];
};

struct _STR_DEV_TIME_{
    time_t time;
};

struct _STR_SYSTEM_OP_{
    char operation;
};

struct _STR_LICAUTH_MODE_{
    int mode;
};

struct _STR_RESOURCE_{
    int freq;
    int BW;
};

struct _STR_LICENSE_INFO_{
    int devID;
    char licvalid;
    int ValidTick;
    char Modulation[4];
    int UserID;
};

//struct _STR_XXXXXXX_{
//
//};

struct _STR_DEV_ID_{
    char devType;
    int devId;
};

struct _STR_BACKUP_{
    char backupMode;    
    char backupState;
};

struct _STR_ALARM_{
    char alarm;
};

struct _STR_EQUIPMENT_FAULT_{
    char fault;
};

#define CFG_RESULT_INFO_MAX 20
struct _STR_CFG_RESULT_{
    int devId;
    int msg;
    char info[CFG_RESULT_INFO_MAX];
};

struct _STR_RESULT_{
    char result;
};

struct _STR_NCS_STATE_{

};

struct _STR_PP_STATE_{

};

struct _STR_MODEM_STATE_{

};


struct rcst_rx{
	struct _STR_CHANNEL_ID_ rxchaId;
	struct _STR_CHANNEL_ID_ txchaId;
};

struct cfg2db_type{
    char cfgtype;
    char set_dbtype;
    char get_dbtype;
};
#pragma pack()

/*declaration functions for ncs_cfg.c*/
int db_open(void);
void db_close(void);
void clear_ppinfo_flag(pNCS_PP_INFO p_pp_info);
void clear_dev_router_flag(pNCS_ROUTE prouter_db);
int get_polling_maxNum(int *num);
int get_polling_interval(int *interval);
int get_cfg_dev_qosrule_by_devId_rcid_flag(int devId,int rc_id,void *pqosrule,int *size,char flag);
int get_cfg_dev_cha_by_devId_chaId(int devId,char chaId,struct _STR_CHANNEL_ *pcha,int *size);
int get_cfg_dev_vlan(int devId,struct _STR_VLAN_ *pvlan,int *size);
int get_pp_by_ip_flag(struct dev_obj *pp,char flag);
int get_modem_info_by_devId_flag(int devId,struct _STR_MODEM_ *pmodem_info,int *size,char flag);
int get_cfg_dev_router_by_devId_rcid_flag(int devId,int rc_id,struct _STR_ADD_ROUTER_ *prouter,int *size,char flag);
int get_cfg_dev_resource_rule_by_devId_flag(int devId,struct _STR_RESOURCE_RULE_ITEMS_ *presourcerule,int *size,char flag);
int get_cfg_dev_resource_bod_by_devId_flag(int devId,struct _STR_RESOURCE_BOD_ *presourcebod,int *size,char flag);
int get_cfg_dev_workmode_by_devId_flag(int devId,struct _STR_WORK_MODE_ *pworkmode,int *size,char flag);
int get_cfg_dev_backup_by_devId_flag(int devId,struct _STR_BACKUP_ *pbackup,int *size,char flag);
int get_cfg_dev_name_by_devId_flag(int devId,struct _STR_DEV_NAME_ *pname,int *size,char flag);
int get_cfg_gwip_by_devId_flag(int devId,struct _STR_DEFAULT_GWIP_ *pgwip,int *size,char flag);
int get_cfg_dev_cha_by_devId_rcid_flag(int devId,int rc_id,struct _STR_CHANNEL_ *pcha,int *size,char flag);
int get_cfg_dev_channel_comm_by_devId_flag(int devId,struct _STR_CHANNEL_COMM_ *pcha_comm,int *size,char flag);
int get_cfg_dev_routers_by_devId(int devId,struct _STR_ADD_ROUTER_ **prouters,int *size);
int get_cfg_dev_qosrules_by_devId(int devId,struct qosrule_items **pqosrule,int *size);
int get_cfg_dev_info_by_devId(int devId,pNCS_MODEM_INFO pdevinfo);
int get_cfg_ipaddr_by_devId_flag(int devId,struct _STR_IPADDR_ *pipaddr,int *size,char flag);
int ncs_cfg_add_network(struct list *tlvList, int *devID);
int ncs_cfg_add_pp(struct list *tlvList, int *devID);
int ncs_cfg_add_modem(struct list *tlvList, int *devID, char *devType);
int ncs_cfg_logoff(struct list *tlvList, int *devID, char *chaID);
int set_ncsfsm_state(struct ncs_object *ncs);
int set_modemfsm_state(struct dev_obj *modem);
int set_ppfsm_state(struct dev_obj *pp);
int set_cfg_dev_equipfualt_by_devId(int devId,char fault);
int get_cfg_pp_info(pNCS_PP_INFO p_ppinfo);
int get_cfg_sig_info(pNCS_MODEM_INFO p_siginfo);
int get_cfg_rx_info(pNCS_MODEM_INFO p_rxinfo);
int get_cfg_rcst_info(pNCS_MODEM_INFO p_rcstinfo);
int get_cfg_ncs_info(pNCS_INFO p_ncsinfo);
int get_cfg_dev_ib(struct _STR_CARRIER_ *ib);
void del_cfg_pp_db(struct dev_obj *pp);
void del_cfg_modem_db(int devId);
void del_router_by_rc_id(pNCS_ROUTE prouter);
void del_wrokmode_by_rcid(pNCS_MODEM_WORKMODE p_workmode);
void del_rource_bod_by_rcid(pNCS_MODEM_RES_BOD p_resourcebod);
void del_qosrule_by_rcid(pNCS_QOS_RULE p_qosrule);
void del_resourcerule_by_rcid(pNCS_MODEM_RES_FIXED p_resourcerule);


#endif



